package com.person.webdemoproject2.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.person.webdemoproject2.mapper.UploadMapper;
import com.person.webdemoproject2.pojo.*;
import com.person.webdemoproject2.utils.sendNoticeUtils;
import com.person.webdemoproject2.utils.jwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UploadServiceimpl implements UploadService {
    @Autowired
    UploadMapper uploadMapper;
    @Autowired
    sendNoticeUtils sendNoticeUtils;
    @Override
    public boolean uploadtext(Text text,String token) {

        if (text.getTextcontent()==null){
            return false;
        }

        String jwt = token;


        try {
            jwtUtils.parseJWT(jwt);
        } catch (Exception e) {
            return false;
        }
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        text.setUserid(userid);
        LocalDateTime now = LocalDateTime.now();
        text.setCreate_time(now);
        text.setUpdate_time(now);


        uploadMapper.uploadtext(text);

        return true;
    }

    @Override
    public void uploadphoto(String url, String token) {

        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        User user = new User();
        user.setId(userid);
        user.setPicture(url);

        uploadMapper.uploadphoto(user);


    }

    @Override
    public void uploadpost(List<String> images, String videoUrl, String token, Post post) {
        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        post.setUserid(userid);
        post.setCreate_time(LocalDateTime.now());
        post.setUpdate_time(LocalDateTime.now());
        post.setVideo_url(videoUrl);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String imageUrlJson = objectMapper.writeValueAsString(images);
            post.setImage_url(imageUrlJson); // 设置为 JSON 字符串
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            throw new RuntimeException("Failed to convert images to JSON", e);
        }
        uploadMapper.uploadpost(post);
    }

    @Override
    public void uploadComment(Comment comment, String token) {
        String jwt = token;
        jwtUtils.parseJWT(jwt);
        Claims claims = jwtUtils.parseJWT(jwt);
        Integer  userid = (Integer) claims.get("id");

        comment.setUserid(userid);
        comment.setCreate_time(LocalDateTime.now());
        comment.setUpdate_time(LocalDateTime.now());
        uploadMapper.uploadComment(comment);
        Integer user = comment.getPostid();
        String content="有用户评论你的帖子:"+comment.getComment();
        String type = "Comment";
        sendNoticeUtils.sendNotification(user,userid,type,content);


    }

    @Override
    public void uploadreComment(reComment recomment, String token) {
        Integer userId = jwtUtils.getUserId(token);
        recomment.setUserid(userId);
        recomment.setCreate_time(LocalDateTime.now());
        recomment.setUpdate_time(LocalDateTime.now());

        uploadMapper.uploadreComment(recomment);
    }

}
